package jmetal.util.dir;

import jmetal.core.Solution;
import jmetal.core.SolutionSet;
import jmetal.util.MatrixUtil;

public class NearestSolutionFitness {
	
	private SolutionSet worstSet;
	
	
	public NearestSolutionFitness(SolutionSet remainFront) {
		this.worstSet = remainFront;
	}


	/**
	 * 为每个解设置最近解距离
	 * @param worstSet
	 * @param population
	 * @param size
	 */
	 public void assignFitness(double[] zideal,double[] intercepts, SolutionSet population) {
		for(int i = 0;i<worstSet.size();i++){
			Solution indiv = worstSet.get(i);
			double minAngle = Double.MAX_VALUE;
			double[] normIndiv = norm(indiv, zideal, intercepts);
			for(int j = 0;j<population.size();j++){
				Solution indiv2 = population.get(j);
				double[] normIndiv2 = norm(indiv2, zideal, intercepts);
				if (indiv2 != indiv) {
					double tempAngle = MatrixUtil.sin(normIndiv2, normIndiv);
				
					if(tempAngle<minAngle)
						minAngle = tempAngle;
				}
			}
			indiv.setNearestSolDis(minAngle);
		}
	 }


	private double[] norm(Solution indiv, double[] zideal, double[] intercepts) {
		double[] normVector = new double[indiv.getNumberOfObjectives()];
		for(int i = 0;i<indiv.getNumberOfObjectives();i++){
			normVector[i] = (indiv.getObjective(i)-zideal[i])/(intercepts[i]-zideal[i]);
		}
		return normVector;
	}
}
